<template>
	<view>
		<view  style=" margin-left: 16rpx;">
			<text style="font-size: 25rpx; ">会员权限：会员免费观看指定的10位讲师课件！</text>
		</view>
		<view style="margin-top: 16rpx; margin-left: 16rpx;">
			<text  style="font-weight: bold;"> 选择会员类型： </text>
		</view>
		
		<view class="addMoney">
			<view class="moneyLaut" >
					<view class="money" v-for="(item,index) in moneyAll" @click="addmoneyValue" :key="index"
						:class="'index ' + (id == item.id ? 'active' : 'default')" :data-item="item">
						<text style="font-size: 40rpx;">{{item.day}}</text><text style="font-size: 40rpx;">天</text> <text class="selfinputtext" style="color: green;margin-left:10rpx;">(</text>  <text class="selfinputtext" style="color: green;">{{item.money}}</text> <text class="selfinputtext" style="color: green;">)元</text>
						
					</view>
			</view>
			
			<view style="margin-top: 20rpx;">
				<view  @tap="chooseAteacher" style="display: flex;align-items: center;margin-left: 425rpx;">
					<image v-if="chooseAlecturerUrl.length == 10" src="/static/iconfont/wancheng.png" style="width: 40rpx; height: 40rpx;"></image>
					<image v-if="chooseAlecturerUrl.length != 10" src="/static/iconfont/weixuanzhong.png" style="width: 40rpx; height: 40rpx;"></image>
					<text style="font-size: 30rpx;color:#1cafff;">指定讲师({{chooseAlecturerUrl.length}}/10)</text>
				</view>
				
				<view @tap="chooseAteacher" style="flex-wrap: wrap;display: flex;justify-content: space-between;margin-left: 10%;margin-top: 16rpx;">
					<block v-for="(chooseAlecturerUrl, index) in chooseAlecturerUrl" :key="index" >
						<view style="width: 20%;margin-bottom: 10px;">
							<image style="height: 70rpx;width: 70rpx;" :src="chooseAlecturerUrl"></image>
						</view>
					</block>
				</view>
			</view>
			
			
			<view class="paymentmethod" style="margin-top: 20rpx;width: 90%;margin-left: 5%; height:140rpx;">
				<view style="margin-top: 10rpx;display: flex;align-items: center;width: 100%;">
					<image  src="/static/iconfont/wxlogo.png" style="width: 70rpx; height: 70rpx;"></image>
					<text  style="margin-left: 16rpx;font-size: 40rpx;">微信支付</text>
					<text v-if="rechargeAmount !== null" style="margin-left: 60rpx;">{{rechargeAmount}}元</text>	
				</view>
				<view style="margin-left:440rpx;margin-top: 16rpx;" >
					<text style="color:#1cafff;" @tap="paymentMethod">更换充值方式></text>
				</view>
			</view>
			
	
			<view @tap="confirmrRecharge" class="querencz" style="border-radius: 16rpx;align-items: center; display: flex;justify-content: center;margin-top: 60rpx;width: 90%;margin-left: 5%; height:80rpx; background-color: #1cafff;">
					<text style="color: white;">确认充值</text>
			</view>
			<view style="margin-top: 30rpx;">
				<text style="font-size: 25rpx; color: #bfbfbf;">
					
						注：支付成功后，如果页面不自动跳转，直接返回即可
				</text>
			
			</view>
			
		</view>
		
		

	</view>
</template>

<script>
	const app = getApp();
	var urlPrefix = app.globalData.urlPrefix;
	export default {
		data() {
			return {
				userAmt:0,
				isMember:0,
				id:null,
				orderDescribe:'开通会员',
				rechargeAmount:null,
				moneyAll:[],
				chooseAlecturerUrl:[]
			}
		},
		onShow() {
			uni.showLoading({
				  title: '加载中',
			});
			
			var urlList = uni.getStorageSync('chooseAlecturerUrl');
			this.chooseAlecturerUrl = urlList
			uni.hideLoading();
		},
		   
		onLoad(options) {
			uni.showLoading({
				  title: '加载中',
			});
			
			this.isMember = options.isMember;
			if(options.isMember == 1){
				this.orderDescribe = "续费会员"
			}
			
			var urlList = uni.getStorageSync('chooseAlecturerUrl');
			this.chooseAlecturerUrl = urlList
			
			var userId = uni.getStorageSync('userId');
			var token = uni.getStorageSync('token');
			let that = this;
				uni.request({
					url: urlPrefix + '/api/applet/myInfo/membershipType',
					method: 'GET',
					header: {
						'content-type': 'application/json',
						'token': token
					},
					success: function (res) {
						uni.hideLoading();
						that.setData({
						   moneyAll:res.data.datas
						});
					}
				});
		},
		methods: {
			
			chooseAteacher:function(){
				uni.navigateTo({
					url: "/pages/chooseAteacher/chooseAteacher"
				})
			},
			
			confirmrRecharge:function(res){
				if (this.rechargeAmount == null) {
				    uni.showToast({
				        title: '请选择会员类型',
				        icon: 'none',
				        duration: 2000
				    });
				    return;
				}
				
				if (this.chooseAlecturerUrl.length==0) {
				    uni.showToast({
				        title: '请指定10位讲师',
				        icon: 'none',
				        duration: 2000
				    });
				    return;
				}
				if (this.chooseAlecturerUrl.length>0 & this.chooseAlecturerUrl.length<10) {
				    uni.showToast({
				        title: '当前指定讲师不足10位',
				        icon: 'none',
				        duration: 2000
				    });
				    return;
				}
				var id = this.id
				var rechargeAmount = this.rechargeAmount
				var orderDescribe = this.orderDescribe
				
				uni.showModal({
				    title: '操作提示',
				    content: '指定讲师后不可修改，是否已确认?',
				    success(res) {
				        if (res.confirm) {
				            let that = this;
				            var userId = uni.getStorageSync('userId');
				            let  chooseAlecturer = uni.getStorageSync('chooseAlecturer');
				           
				            let chooseAlecturerList = chooseAlecturer.join(',');
				            uni.request({
				                url: urlPrefix + '/api/app/recharge/memberCall?id=' + id + '&userId=' + userId + "&money="+rechargeAmount + "&orderDescribe="+orderDescribe+"&chooseAlecturerList="+chooseAlecturerList,
				                method: 'GET',
				                header: {
				                    'content-type': 'application/json',
				                    'token': uni.getStorageSync('token')
				                },
				                success: function (res) {
				                    if (res.data.resp_code == 0) {
				           			 
				            		        uni.hideLoading();
				           		   
				           				uni.requestPayment({	
				           					
				           				     "provider": "wxpay", 
				           					 "orderInfo": {
				           						 "appid":  res.data.datas.appid,  // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
				           						 "noncestr":  res.data.datas.noncestr, // 随机字符串
				           						 "package": "Sign=WXPay",        // 固定值
				           						 "partnerid": res.data.datas.partnerid,      // 微信支付商户号
				           						 "prepayid": res.data.datas.prepayid, // 统一下单订单号 
				           						 "timestamp":res.data.datas.timestamp,        // 时间戳（单位：秒）
				           						 "sign": res.data.datas.sign// 签名，这里用的 MD5/RSA 签名
				           					 },
				           					 success(res) {
				           						 uni.navigateBack({
				           						             delta: 1
				           						 });
				           					 },
				           					 fail(e) {
				           						 uni.showToast({
				           						     title: '支付失败',
				           						     icon: 'none',
				           						     duration: 2000
				           						 });
				           					 }
				           				})
				           		
				                      
				                    }else {
				            			uni.showToast({
				            			    title: res.data.resp_msg,
				            			    icon: 'none',
				            			    duration: 2000
				            			});
				            		}
				                
				           	}
				           });
				        } else if (res.cancel) {
				            //这里是点击取消执行事件
				        }
				    }
				});
			},
			paymentMethod:function(res){
				uni.showToast({
				    title: '当前仅支持微信充值',
				    icon: 'none',
				    duration: 2000
				});
				
			},
			addmoneyValue:function(res) {
				this.id = res.currentTarget.dataset.item.id;
				this.rechargeAmount = res.currentTarget.dataset.item.money;	
			},
		
		}
	}
</script>

<style>
@import './applyFor.scss';
</style>
